<?php

declare(strict_types=1);

namespace App\Controller;

use App\Constants\ErrorCode;
use App\Annotation\EsUp;
use App\Service\FlyShopCashOrderService;
use App\Service\FlyShopService;
use App\Service\PayService;
use Hyperf\DbConnection\Db;
use Hyperf\Validation\Rule;
use Psr\Http\Message\ResponseInterface;
use Hyperf\Di\Annotation\Inject;

/**
 * 商家信息
 * Class ShopCashController
 * @package App\Controller
 */
class ShopInfoController extends AbstractController
{
    /**
     * @Inject
     * @var FlyShopService
     */
    private $shopService;


    /**
     * 登陆商家用户ID
     * @return int
     */
    public function get_user_id(): int
    {
        return (int)$this->getAdminId();
    }

    /**
     * 获取当前登录用户商家详情
     * @return ResponseInterface
     *
     */
    public function info()
    {
        $user_id = $this->get_user_id();
        $lat = $this->request->input('lat','');
        $lng = $this->request->input('lng','');
        $this->validation([
                'lng'=>$lng,
                'lat'=>$lat,
             ],
            [
                'lng'=> 'numeric|min:0|max:180',
                'lat'=> 'numeric|min:-90|max:90',
            ],
        );
        return $this->jsonResponse->success($this->shopService->getShopInfoByUserId($user_id,$lng,$lat));
    }
    /**
     * 获取指定shop_id商家详情
     * @return ResponseInterface
     * @EsUp(action="shop_product_add",type=1)
     */
    public function shop_info()
    {
        $shop_id = $this->request->input('shop_id',0);
        $lat = $this->request->input('lat','');
        $lng = $this->request->input('lng','');
        $this->validation(
            [
                'shop_id'=>$shop_id,
                'lng'=>$lng,
                'lat'=>$lat
            ],
            [
                'shop_id' => [
                    "required",
                    "integer",
                    "min:0",
                    Rule::unique('fly_shop','id')
                        ->where('shop_state',0)
                        ->where('food_state',0)
                        ->whereNull('delete_time')
                ],
                'lng'=> 'numeric|min:0|max:180',
                'lat'=> 'numeric|min:-90|max:90',
            ],
            [
                'shop_id.unique' => '店铺不存在或未审核'
            ]
        );
        return $this->jsonResponse->success($this->shopService->getShopInfo(intval($shop_id),$lng,$lat));
    }

    /**
     * 设置店铺地址
     * @return ResponseInterface
     * @EsUp(action="shop_add",type=1)
     */
    public function setShopAddress(): ResponseInterface
    {
        $shop = $this->shopService->getMyShop($this->get_user_id());
        if (empty($shop)) {
            $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '店铺不存在');
        }
        $params = $this->request->all();
        $params['shop_id'] = $shop['id'];
        $addr_id = $this->shopService->setShopAddress($params);
        return $addr_id ? $this->jsonResponse->success(['address_id' => $addr_id], 'success!') : $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, 'failed!');
    }

    /**
     * 获取店铺地址
     * @return ResponseInterface
     */
    public function getShopAddress(): ResponseInterface
    {
        $shop = $this->shopService->getMyShop($this->get_user_id());
        if (empty($shop)) {
            $this->jsonResponse->success([], '店铺不存在');
        }
        return $this->jsonResponse->success($this->shopService->getShopAddress($shop['id']), 'success!');
    }

    /**
     * 删除商家地址
     * @return ResponseInterface
     */
    public function delShopAddress(): ResponseInterface
    {
        $shop = $this->shopService->getMyShop($this->get_user_id());
        if (empty($shop)) {
            $this->jsonResponse->success([], '店铺不存在');
        }
        return $this->shopService->delShopAddress($shop['id'], (int)$this->request->input('id', 0))
            ? $this->jsonResponse->success([], '操作成功')
            : $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '操作失败');
    }

}